<template>
	<view class="container">
		<view class="user_pic">
			<image :src="avatar" alt="" @click="uploadImg" />
		</view>
		<view class="content">
			<view class="list">
				<view class="info_text">昵称</view>
				<view class="info_input">
					<input type="text" placeholder="请输入昵称" v-model="nickname">
				</view>
			</view>
			<view class="list">
				<view class="info_text">性别</view>
				<view class="info_input">
					<input type="text" placeholder="请选择性别" v-model="sex_han" disabled="" @click="sexShow = true">
				</view>
			</view>
			<view class="list">
				<view class="info_text">生日</view>
				<view class="info_input">
					<input type="text" placeholder="请输入生日" v-model="birthday" disabled="" @click="timeShow = true">
				</view>
			</view>
			<view class="list">
				<view class="info_text">常住地址</view>
				<view class="info_input">
					<input type="text" placeholder="请输入常住地址" v-model="address">
				</view>
			</view>
		</view>
		<view class="submit" @click="submit">
			提交
		</view>
		<u-select v-model="sexShow" :list="sexList" @confirm="sexConfirm"></u-select>
		<u-picker mode="time" v-model="timeShow" @confirm="timeConfirm"></u-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: null,
				nickname: '',
				avatar: '',
				sex: '',
				sex_han: '',
				birthday: '',
				address: '',
				sexShow: false,
				timeShow: false,
				sexList: [{
						label: '保密',
						value: 0
					},
					{
						label: '男',
						value: 1
					},
					{
						label: '女',
						value: 2
					},
				],
			}
		},
		onLoad() {
			this.getData()
		},
		methods: {
			submit() {
				let parmas = {
					nickname: this.nickname,
					avatar: this.avatar,
					sex: this.sex,
					birthday: this.birthday,
					address: this.address,
				}
				this.$allApi.updaeUserInfo(parmas).then(res => {
					this.$u.toast('更新成功')
					setTimeout(() => {
						this.getData()
					}, 500)
				})
			},
			sexConfirm(e) {
				console.log(e);
				this.sex = e[0].value
				this.sex_han = e[0].label
			},
			timeConfirm(e) {
				console.log(e);
				this.birthday = e.year + '-' + e.month + '-' + e.day
			},
			uploadImg() {
				uni.chooseImage({
					count: 1,
					success: (res) => {
						for (let item of res.tempFiles) {
							if (res.tempFiles[0].size > item.size) {
								this.$u.toast('图片大小不能超过5MB，请检查')
								return
							}
							uni.uploadFile({
								url: this.$baseUrl + 'api/upload/image',
								filePath: item.path,
								name: 'file',
								header: {
									'Authorization': 'Bearer ' + uni.getStorageSync('userToken') || ''
								},
								success: (imgRes) => {
									this.avatar = JSON.parse(imgRes.data).data.url
								}
							})

						}
					}
				})
			},
			getData() {
				this.$allApi.getUserInfo().then(res => {
					console.log(res.data);
					this.nickname = res.data.nickname
					this.avatar = res.data.avatar
					this.sex = res.data.sex
					this.sex_han = res.data.sex == 0 ? '保密' : res.data.sex == 1 ? '男' : '女'
					this.birthday = res.data.birthday
					this.address = res.data.addres
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.submit {
		display: flex;
		align-items: center;
		justify-content: center;
		color: #fff;
		font-size: 28rpx;
		width: 710rpx;
		margin: 0 auto;
		background: #EA3D37;
		border-radius: 40rpx;
		height: 80rpx;
		margin-top: 80rpx;
	}

	.content {
		background: #fff;
		padding: 30rpx;
	}

	.list {
		display: flex;
		align-items: center;
		justify-content: flex-start;
		margin-bottom: 20rpx;

		.info_text {
			width: 120rpx;
		}

		.info_input {
			flex: 1;
			display: flex;
			padding-left: 40rpx;

			input {
				flex: 1;
				padding: 20rpx;
				border-bottom: 1px solid #eee;
			}
		}
	}

	.user_pic {
		display: flex;
		align-items: center;
		justify-content: center;

		image {
			width: 184rpx;
			height: 182rpx;
			border-radius: 50%;
		}
	}

	.container {
		padding: 20rpx;
		min-height: 100vh;
		background: #f8f8f8;
	}
</style>